<template>
  <view class="heart_model">
    <view class="heart_container">
      <text class="iconfont icon-keaide"> 心动模式正在开启......</text>
      
    </view>
    </view>
</template>

<script>
import { mapState } from 'vuex'
export default {
  onLoad() {
    if (this.userInfo.nickname) {
      this.getLoveSongs(this.userInfo.userId, this.cookie)
      this.getCollectSongList(this.userInfo.userId)
      setTimeout(() => {
        this.getHeartModelList(
          this.loveSongsId[Math.floor(Math.random() * this.loveSongsId.length)],
          this.collectSongList[Math.floor(Math.random() * this.collectSongList.length)].id,
          this.cookie
        )
      }, 1000)
    } else if (this.orUserinfo.nickname) {
      this.getLoveSongs(this.orUserinfo.userId, this.cookie)
      this.getCollectSongList(this.orUserinfo.userId)
      setTimeout(() => {
      this.getHeartModelList(
        this.loveSongsId[Math.floor(Math.random() * this.loveSongsId.length)],
        this.collectSongList[Math.floor(Math.random() * this.collectSongList.length)].id,
        this.cookie
      )
      }, 1000)
    }
  },
  computed: {
    // 以下两种方式都可以
    // ...mapState({ token: state => state.login.token })
    ...mapState('login', ['cookie', 'userInfo', 'orUserinfo'])
  },
  data() {
    return {
      collectSongList: [],
      loveSongsId: []
    }
  },
  methods: {
    async getCollectSongList(id) {
      const { data: res } = await uni.$http.get('user/playlist', { uid: id })
      if (res.code !== 200) return uni.$showMsg('收藏列表获取失败')
      // console.log(res)
      this.collectSongList = res.playlist
    },
    async getLoveSongs(uid, cookie) {
      const { data: res } = await uni.$http.get('likelist', { uid, cookie })
      // console.log(res)
      if (res.code !== 200) return uni.$showMsg('喜欢歌曲列表获取失败')
      this.loveSongsId = res.ids
    },
    async getHeartModelList(id, pid, cookie) {
      const { data: res } = await uni.$http.get('playmode/intelligence/list', { id, pid, cookie })
      // console.log(res);
      if (res.code !== 200) return uni.$showMsg('收藏列表获取失败')
      setTimeout(() => {
        uni.$emit('vals', '心动')
      }, 1000)
      uni.navigateTo({
        url: '/subpkg/songs_detail/songs_detail?id=' + res.data[Math.floor(Math.random() * res.data.length)].id
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.heart_model {
  position: relative;
  background: linear-gradient(45deg, #a28bc3, #9f93c5, #ba8aaf, #af81a0);
  height: 100vh;
  background-size: 100% 100%;
  
}
.heart_container{
  position: absolute;
  text-align: center;
  // margin: 0 auto;
  top: 30%;
  left: 0%;
  right: 0%;
  bottom: 0%;
}
</style>
